﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace MaxZhang.EasyEntities.EasyDesigner.Core
{
    [Serializable]
    public class DbProperty
    {
        [XmlAttribute("Name")]
        public string Name { set; get; }
        [XmlAttribute("Memo")]
        public string Memo { set; get; }
        [XmlAttribute("IsPrimaryKey")]
        public bool IsPrimaryKey { set; get; }
        [XmlAttribute("IsIdentity")]
        public bool IsIdentity { set; get; }
        [XmlAttribute("IdentitySpeed")]
        public int IdentitySpeed { set; get; }

        [XmlAttribute("IdentitySetp")]
        public int IdentitySetp { set; get; }

        [XmlAttribute("DataType")]
        public string DataType { set; get; }

        [XmlAttribute("DataLength")]
        public int  DataLength { set; get; }

        [XmlAttribute("DataDotLength")]
        public int DataDotLength { set; get; }

        [XmlAttribute("IsNull")]
        public bool IsNull { set; get; }
        [XmlAttribute("DefaultValue")]
        public string DefaultValue { set; get; }
        [XmlIgnore]
        public Type PropertyType { set; get; }
        [XmlAttribute("PropertyType")]
        public string PropertyTypeName { set; get; }

        public string GetPropertyTypeName()
        {
            if (this.IsNull && this.PropertyType.IsValueType)
                return string.Format("Nullable<{0}>", this.PropertyType.Name);
            else
            {
                return PropertyType.Name;
            }
        }

        public string GetPropertyName()
        {
            return GetName() + "Property";
        }

        public string GetName()
        {
            return DbEntity.Pn(Name);
        }

        public string GetAttribute()
        {
            StringBuilder attr = new StringBuilder();
            attr.Append("[Column(Name=\"").Append(this.Name).Append("\"");
            if (this.IsPrimaryKey)
                attr.Append(",IsPrimaryKey=true");
            if(this.IsNull)
                attr.Append(",IsNullable=true");
            if(this.IsIdentity)
                attr.Append(",IsDbGenerated=true");
            attr.Append(")]");
            return attr.ToString();
        }

    }
}
